package com.wolfalpha.utron.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wolfalpha.utron.R;

/**
 * 自定义的对话框
 *
 * @author
 */
public class SortAlertDialog implements View.OnClickListener {
    private Context context;
    private android.app.AlertDialog ad;
    private TextView titleView;
    private RelativeLayout messageView;

    private LinearLayout hot;
    private LinearLayout time;
    private LinearLayout rand;

    private LayoutInflater inflater;

    public SortAlertDialog(Context context) {
        // TODO Auto-generated constructor stub
        this.context = context;
        inflater = LayoutInflater.from(context);
        ad = new android.app.AlertDialog.Builder(context).create();
        ad.show();
        //关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
        Window window = ad.getWindow();
        window.setContentView(R.layout.dialog_main_sort);
        titleView = (TextView) window.findViewById(R.id.tv_dialog_base_title);
        messageView = (RelativeLayout) window.findViewById(R.id.rl_dialog_base_message);

        hot = (LinearLayout) window.findViewById(R.id.ll_main_idea_sort_hot);
        time = (LinearLayout) window.findViewById(R.id.ll_main_idea_sort_time);
        rand = (LinearLayout) window.findViewById(R.id.ll_main_idea_sort_rand);

        hot.setOnClickListener(this);
        time.setOnClickListener(this);
        rand.setOnClickListener(this);
    }

    /**
     * 设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        titleView.setText(title);
    }

    private OnButtonClickedListener mListener;

    public interface OnButtonClickedListener {
        public void click(int which);
    }

    public void setOnButtonClickedListener(OnButtonClickedListener listener) {
        mListener = listener;
    }

    /**
     * 关闭对话框
     */
    public void dismiss() {
        ad.dismiss();
    }

    @Override
    public void onClick(View v) {
        if (mListener == null) return;
        switch (v.getId()) {
            case R.id.ll_main_idea_sort_hot:
                mListener.click(1);
                break;
            case R.id.ll_main_idea_sort_time:
                mListener.click(2);
                break;
            case R.id.ll_main_idea_sort_rand:
                mListener.click(3);
                break;
        }
        dismiss();
    }
}